<template>
  <div>
    <van-nav-bar title="账号注册" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '用户名不能小于5个数' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码不能小于6个数' }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>

    <router-link class="box" to="/login">已有账号，去登录~</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user.js'
export default {
  name: '',
  props: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      //   console.log('submit', values)
      const data = await register(values)
      console.log(data)
      if (data.status === 200) {
        this.$toast.success(data.description)
      } else {
        return this.$toast.fail(data.description)
      }
      this.$router.push('/login')
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped lang="less">
.box {
  margin-left: 30vw;
}
</style>
